#!/bin/bash
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   runtest.sh of /tools/systemtap/Sanity/kernel-modules
#   Description: Tests systemtap working with kernel modules
#   Author: Petr Muller <pmuller@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   Copyright (c) 2012 Red Hat, Inc. All rights reserved.
#
#   This copyrighted material is made available to anyone wishing
#   to use, modify, copy, or redistribute it subject to the terms
#   and conditions of the GNU General Public License version 2.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE. See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh

PACKAGE="systemtap"

check_probes(){
  FAIL=""
  rlAssertGrep init_module $1 	 || FAIL='yes'
  rlAssertGrep cleanup_module $1 || FAIL='yes'
  rlAssertGrep some_method $1	 || FAIL='yes'

  if [ -n "$FAIL" ]
  then
    rlFileSubmit $1 $2.out
  fi
}

rlJournalStart

rlPhaseStartSetup "Preparing modules"
  rlRun "mkdir -p 'stap-underscore' 'stap-dash' 'stap-none'"
  rlRun "cp module.c stap-underscore/test_module.c"
  rlRun "cp module.c stap-dash/test-module.c"
  rlRun "cp module.c stap-none/testmodule.c"

  cat Makefile.template | sed -e 's/MODULENAME/test_module/g' \
                        | sed -e "s|DIRECTORY|$PWD/stap-underscore|g" > stap-underscore/Makefile
  rlAssert0 "Creating Makefile for underscore variant" $?
  cat Makefile.template | sed -e 's/MODULENAME/test-module/g' \
                        | sed -e "s|DIRECTORY|$PWD/stap-dash|g" > stap-dash/Makefile
  rlAssert0 "Creating Makefile for dash variant" $?

  cat Makefile.template | sed -e 's/MODULENAME/testmodule/g' \
                        | sed -e "s|DIRECTORY|$PWD/stap-none|g" > stap-none/Makefile
  rlAssert0 "Creating Makefile for no character variant" $?

  if [ -n "$ARCH" ]
  then
    rlLog "ARCH is set to: [$ARCH]"
    rlLog "This interferes with 'make module', so it needs to be unset."
    ARCHOLD="$ARCH"
    unset ARCH
    rlLog "ARCH is set to: [$ARCH]"
  fi
  rlRun "make -C stap-underscore module"
  rlRun "make -C stap-dash module"
  rlRun "make -C stap-none module"
  if [ -n "$ARCHOLD" ]
  then
    rlLog "Restoring ARCH"
    export ARCH="$ARCHOLD"
    rlLog "ARCH is set to: [$ARCH]"
  fi

rlPhaseEnd

rlPhaseStartTest "Testing underscore variant"
  rlRun "cp stap-underscore/test_module.ko /lib/modules/$(uname -r)/"
  rlRun "insmod stap-underscore/test_module.ko"
  stap -L 'module("test_module").function("*")' -v > stap.out
  check_probes stap.out "module-und-probe-und"
  stap -L 'module("test-module").function("*")' -v > stap.out
  check_probes stap.out "module-und-probe-dash"
  sleep 1
  rlRun "rmmod test_module"
  rlRun "rm -f /lib/modules/$(uname -r)/test_module.ko"
rlPhaseEnd

rlPhaseStartTest "Testing dash variant"
  rlRun "cp stap-dash/test-module.ko /lib/modules/$(uname -r)/"
  rlRun "insmod stap-dash/test-module.ko"
  stap -L 'module("test_module").function("*")' -v > stap.out
  check_probes stap.out "module-dash-probe-und"
  stap -L 'module("test-module").function("*")' -v > stap.out
  check_probes stap.out "module-dash-probe-dash"
  sleep 1
  rlRun "rmmod test-module"
  rlRun "rm -f /lib/modules/$(uname -r)/test-module.ko"
rlPhaseEnd

rlPhaseStartTest "Testing no separator variant"
  rlRun "cp stap-none/testmodule.ko /lib/modules/$(uname -r)/"
  rlRun "insmod stap-none/testmodule.ko"
  stap -L 'module("testmodule").function("*")' -v > stap.out
  check_probes stap.out "module-none-probe-none"
  sleep 1
  rlRun "rmmod testmodule"
  rlRun "rm -f /lib/modules/$(uname -r)/testmodule.ko"
rlPhaseEnd

rlPhaseStartCleanup
  rlRun "rm -rf stap-underscore stap-dash stap-none stap.out"
rlPhaseEnd
rlJournalPrintText
rlJournalEnd
